﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Think.Lenovo.Com.Models;
using MySql.Data.MySqlClient;

namespace Think.Lenovo.Com.DataAccess
{
    public class RegionInfo
    {
        /// <summary>
        /// 得到大的分区
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllRegion()
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select  * from tbl_region   where level=2";
            DataSet ds = new DataSet();
            ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            return ds;
        }


        /// <summary>
        /// 得到分销商的分区
        /// </summary>
        /// <param name="regionid"></param>
        /// <returns></returns>
        public DataSet GetFenXiaoRegion(int regionid)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "Select id,nodename from tbl_region   where level=3 and id = " + regionid;
            DataSet ds = new DataSet();
            ds = MySqlHelper.ExecuteDataset(conn, sql.ToString());
            return ds;
        }

        /// <summary>
        /// 获取国内所有省
        /// </summary>
        /// <returns></returns>
        public LinkedList<RegionModel> GetAllProvince() 
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder();
            sql.Append("Select * From tbl_region where level=3");
            DataSet ds = MysqlHelper.Query(conn,sql.ToString());
            LinkedList<RegionModel> list = new LinkedList<RegionModel>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    RegionModel model = new RegionModel();
                    int i = 0;
                    if (!Int32.TryParse(row["Id"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.Id = i;
                    if (!Int32.TryParse(row["Level"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.Level = i;
                    model.NodeName = Convert.ToString(row["NodeName"]);
                    if (!Int32.TryParse(row["ParentId"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.ParentId = i;
                    if (!Int32.TryParse(row["Seq"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.Seq = i;
                    model.Units = Convert.ToString(row["Units"]);
                    list.AddLast(model);
                }
            }
            return list;
        }

        /// <summary>
        /// 根据省id 得到该省下的所有城市
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public LinkedList<RegionModel> GetCitiesByProvince(int provinceId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder();
            sql.Append("Select * From tbl_region where level=4 and parentId="+provinceId);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            LinkedList<RegionModel> list = new LinkedList<RegionModel>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    RegionModel model = new RegionModel();
                    int i = 0;
                    if (!Int32.TryParse(row["Id"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.Id = i;
                    if (!Int32.TryParse(row["Level"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.Level = i;
                    model.NodeName = Convert.ToString(row["NodeName"]);
                    if (!Int32.TryParse(row["ParentId"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.ParentId = i;
                    if (!Int32.TryParse(row["Seq"].ToString(), out i))
                    {
                        i = 0;
                    }
                    model.Seq = i;
                    model.Units = Convert.ToString(row["Units"]);
                    list.AddLast(model);
                }
            }
            return list;
        }

        public RegionModel GetBigAreaByProvinceId(int provinceId)
        {
            RegionModel model = new RegionModel();
            RegionInfo regionInfo = new RegionInfo();
            model = regionInfo.GetModel(provinceId);
            model = regionInfo.GetModel(model.ParentId);
            return model;
        }

        /// <summary>
        /// 根据所在城市id 得到该城市属于哪个大区
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public RegionModel GetBigAreaByCityId(int cityId)
        {
            RegionModel model = new RegionModel();
            model = GetModel(cityId);
            if (model != null && model.ParentId > -1)
            {
                if (model.ParentId == 0)
                {
                    return model;
                }
                else
                {
                    GetBigAreaByCityId(model.ParentId);
                    return model;
                }
            }
            else
            {
                return model;
            }
        }
       
        public RegionModel GetProvinceByCityId(int cityId)
        {
            RegionModel cityModel = new RegionModel();
            cityModel = GetModel(cityId);
            if (cityModel != null && cityModel.ParentId > -1)
            {
                if (cityModel.ParentId  > 0)
                {
                    RegionModel provinceModel = new RegionModel();
                    provinceModel = GetModel(cityModel.ParentId);
                    return provinceModel;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        public RegionModel GetCity(int Id)
        {
            return GetModel(Id);
        }

        public RegionModel GetProvince(int Id)
        {
            return GetModel(Id);
        } 

        public RegionModel GetModel(int Id)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder sql = new StringBuilder(512);
            sql.Append("Select * from tbl_region where id=" + Id);
            DataSet ds = MysqlHelper.Query(conn, sql.ToString());
            RegionModel model = new RegionModel();
            if (ds != null)
            {
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Int32 i;
                        model.Id = Convert.ToInt32(ds.Tables[0].Rows[0]["Id"]);
                        model.Level = Convert.ToInt32(ds.Tables[0].Rows[0]["Level"]);
                        model.NodeName = Convert.ToString(ds.Tables[0].Rows[0]["NodeName"]);
                        model.ParentId = Convert.ToInt32(ds.Tables[0].Rows[0]["parentId"]);
                        model.Seq = Convert.ToInt32(ds.Tables[0].Rows[0]["Seq"]);
                        model.Units = Convert.ToString(ds.Tables[0].Rows[0]["Units"]);
                    }
                }
            }
            return model;
        }
    }

}
